#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/resource.h>

#define BUFFSIZE    100

void (*signal_intr(int sig, void (*func)(int)))(int)
{
    struct sigaction action, oldaction;
    sigset_t mask;

    action.sa_handler = func;
    action.sa_flags = 0;
    action.sa_flags |= SA_INTERRUPT;
    sigemptyset(&mask);
    action.sa_mask = mask;

    sigaction(sig, &action, &oldaction);

    return oldaction.sa_handler;
}

void sig_fsz(int sig)
{
    fprintf(stderr, "catch SIGXFSZ\n");
    return;
}

int main(int argc, char const* argv[])
{
    int n, s;
    char buf[BUFFSIZE];
    struct rlimit rl;

    signal_intr(SIGXFSZ, sig_fsz);

    getrlimit(RLIMIT_FSIZE, &rl);
    
    if (rl.rlim_cur == RLIM_INFINITY)
        printf("infinite\n");
    else
        printf("%ld\n", rl.rlim_cur);

    rl.rlim_cur = 1024;
    setrlimit(RLIMIT_FSIZE, &rl);

    while ((n = read(STDIN_FILENO, buf, BUFFSIZE)) > 0)
        if ((s = write(STDOUT_FILENO, buf, n)) < n)
        {
            fprintf(stderr, "has write %d of %d\n", s, n);
            perror("write error");
            exit(0);
        }

    if (n < 0)
        perror("read error");
    
    return 0;
}
